//
//  ButtonUtil.swift
//  Extend
//
//  Created by 郑浩 on 2017/10/18.
//  Copyright © 2017年 敬信. All rights reserved.
//

import UIKit

public enum ButtonImagePosition: NSInteger {
    
    case PositionTop = 0
    case Positionleft
    case PositionBottom
    case PositionRight
}


public extension UIButton {
    
    ///   动画
    ///
    /// - Parameters:
    ///   - names: 图片名称
    ///   - count: 图片数量
    public func animation(ImageName names: String, ImageCount count: Int) {
        var imageArray = [AnyHashable]()
        for i in 1..<count {
            let name = "\(names)\(i).png"
            let image = UIImage(named: name)
            imageArray.append(image!)
            self.imageView?.animationImages = imageArray as? [UIImage]
            self.imageView?.animationDuration = Double(imageArray.count) * 0.5
            self.imageView?.animationRepeatCount = 0
        }
        self.imageView?.startAnimating()
    }
    
    /// 停止动画
    public func stopAnimation() {
        if (self.imageView?.isAnimating)! {
            self.imageView?.stopAnimating()
        }
    }
    
    
    /**
     imageName:图片的名字
     title：button 的名字
     type ：image 的位置
     Space ：图片文字之间的间距
     */
    
    func imageAndTitle(imageName: String, title: String, type: ButtonImagePosition, Space space: CGFloat)  {
        
        self.setTitle(title, for: .normal)
        self.setImage(UIImage(named:imageName), for: .normal)
        
        let imageWith :CGFloat = (self.imageView?.frame.size.width)!;
        let imageHeight :CGFloat = (self.imageView?.frame.size.height)!;
        
        var labelWidth :CGFloat = 0.0;
        var labelHeight :CGFloat = 0.0;
        
        labelWidth = CGFloat(self.titleLabel!.intrinsicContentSize.width);
        labelHeight = CGFloat(self.titleLabel!.intrinsicContentSize.height);
        
        var  imageEdgeInsets :UIEdgeInsets = UIEdgeInsets();
        var  labelEdgeInsets :UIEdgeInsets = UIEdgeInsets();
        
        switch type {
        case .PositionTop:
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight - space/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
            break;
        case .Positionleft:
            imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
            break;
        case .PositionBottom:
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
            break;
        case .PositionRight:
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
            break;
        }
        
        // 4. 赋值
        self.titleEdgeInsets = labelEdgeInsets
        self.imageEdgeInsets = imageEdgeInsets
    }
    
    /// 按钮渐变
    ///
    /// - Parameters:
    ///   - original: 起始颜色
    ///   - new: 终点颜色
    ///   - type: 方向 0：从上到下 1：从左到右
    func createGradient(_ colors: Array<UIColor>, _ type: Int) {
        let image = UIImage.setGradientImageWithBounds(rect: self.bounds, colors: colors, type: type)
        self.setBackgroundImage(image, for: UIControlState.normal)
    }
    
    ///添加虚线框
    func addVirtualBorder(lineColor: UIColor, lineWidth: CGFloat, lineDashPattern: [NSNumber], corner: CGFloat) {
        //先移除原有的layer
        for item in self.layer.sublayers! {
            if item.isKind(of: CAShapeLayer.self) {
                item.removeFromSuperlayer()
            }
        }
        
        let border = CAShapeLayer()
        //虚线的颜色
        border.strokeColor = lineColor.cgColor
        //填充的颜色
        border.fillColor = UIColor.clear.cgColor
        let path = UIBezierPath(roundedRect: self.bounds, cornerRadius: corner)
        //设置路径
        border.path = path.cgPath
        border.frame = self.bounds
        //虚线的宽度
        border.lineWidth = lineWidth
        //设置线条的样式
        //    border.lineCap = @"square";
        //虚线的间隔
        border.lineDashPattern = lineDashPattern
        self.layer.cornerRadius = corner
        self.layer.masksToBounds = true
        self.layer.addSublayer(border)
    }
    
    ///去掉虚线框
    func removeVirtualBorder() {
        for item in self.layer.sublayers! {
            if item.isKind(of: CAShapeLayer.self) {
                item.removeFromSuperlayer()
            }
        }
    }
}
